package zkk.com.mengqu.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.activity.AddKitchenActivity;
import zkk.com.mengqu.adapter.NearByKitchenAdapter;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.KitchenDetail;
import zkk.com.mengqu.models.KitchenList;

/**
 * Created by Administrator on 2017/6/9.
 * 加入私厨Fragment
 */
public class AddKitchenFragment extends Fragment implements View.OnClickListener{
    private Context context;
    private View view;
    private Button addKitchen;
    private ListView listView;
    private NearByKitchenAdapter adapter;
    private List<KitchenDetail> lists = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_add_kitchen,null);
        initView();
//        getNearByKitchen();
        return view;
    }

    public void initView(){
        context = getActivity();
        addKitchen = (Button)view.findViewById(R.id.btn_add_kitchen);
        listView = (ListView) view.findViewById(R.id.listview);
        adapter = new NearByKitchenAdapter(context , lists);
        listView.setAdapter(adapter);

        addKitchen.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        getNearByKitchen();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add_kitchen:
                startActivity(new Intent(getActivity() , AddKitchenActivity.class));
                break;
        }
    }

    private void getNearByKitchen(){
        APIManager.setManager()
                .getNearByKitchen("nearby_kitchen",113.584945,22.28563,"10KM")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<KitchenList>() {
                    @Override
                    public void onCompleted() {
                        Log.w("onCompleted","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("onError","onError");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(KitchenList kitchenList) {
                        if (kitchenList.getKitchens().size() != 0){
                            lists.clear();
                            Log.w("onNext","" + kitchenList.getResult());
                            lists.addAll(kitchenList.getKitchens());
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
    }
}
